<?php

namespace app\api\model;

use think\Db;

class UserPunish extends Common {
    /**
     * @param $user_id
     * @param $key
     */
    static public function inc($user_id, $key) {
        return false;
        $userPunish = self::get(['user_id' => $user_id]);

        if ($userPunish) {
            $where       = ['id' => $userPunish['id']];
            $where[$key] = $userPunish[$key] + 1;

            self::update($where);
        } else {
            $where       = ['user_id' => $user_id];
            $where[$key] = 1;

            self::create($where);
        }
        self::userQuality($user_id, $key);
    }
    public static function userQuality($user_id, $key = '')
    {
        $userPunishModel = new userPunish;
        $userPunish      = $userPunishModel->master()->where(['user_id' => $user_id])->find();
        $user            = User::get($user_id);
        $activeval       = $user['activeval'];
        $shouyi          = $user['shouyi'];
        $quality         = 0;
        if ($userPunish['age_limit'] == 0 && $userPunish['withdraw_limit'] == 0 && $userPunish['wallet_frozen'] == 0 && $userPunish['search_limit'] == 0 && $userPunish['banned'] == 0 && $userPunish['warn_monitor'] == 0 && $userPunish['complaint'] == 0) {
            if ($activeval > 100) {
                if ($shouyi < 50000) {
                    $quality = 4;
                } else {
                    $quality = 3;
                }
            } else {
                $quality = 2;
            }
        } elseif (($userPunish['complaint'] || $userPunish['search_limit'] || $userPunish['withdraw_limit']) && $shouyi > 50000) {
            $quality = 2;
        // } elseif ($userPunish['banned']) {
        //     $quality = 1;
        // }
        // } elseif ($userPunish['age_limit'] || $userPunish['banned'] || $userPunish['warn_monitor']) {
        //     $quality = 1;
        // }
        // $userConfigPersonal = ConfigPersonal::get(['user_id'=>$user_id]);
        // if ($userConfigPersonal) {
        //     if ($userConfigPersonal['chat_price'] >= 5 || $userConfigPersonal['voice_price'] >= 35 || $userConfigPersonal['video_price'] >= 50) {
        //         $quality = 1;
        //     }
        }
        if ($quality && $quality < $user['quality']) {
            User::update(['id' => $user_id, 'quality' => $quality]);

            if ($key) {
                $keys = [
                    'age_limit'=>'年龄限制触发',
                    'withdraw_limit'=>'提现限制触发',
                    'wallet_frozen'=>'冻结积分触发',
                    'search_limit'=>'限制搜索触发',
                    'banned'=>'封号触发',
                    'warn_monitor'=>'敏感监听触发',
                    'complaint'=>'被投诉触发',
                ];

                $qualitys = ['未检出','低端','一般','优质','超优质'];

                $text = ($keys[$key]??'') . '更新用户质量：'.$qualitys[$user['quality']].'=>'.$qualitys[$quality];

                Db::name('admin_logs')->insert(['channel'=>3,'admin'=>'系统','user_id'=>$user_id,'content'=>$text]);
            }
        }
    }
}
